package com.csist.jobtitleevaluation.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csist.jobtitleevaluation.mapper.AnnouncementMapper;
import com.csist.jobtitleevaluation.po.Announcement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 公告 前端控制器
 * </p>
 *
 * @author chun
 * @since 2021-12-02
 */
@Controller
@RequestMapping("/announcement")
public class AnnouncementController {

    @Autowired
    private AnnouncementMapper announcementMapper;

    /**
     * 最新公告
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/newAnnouncement")
    public List<Announcement> newAnnouncement() {
        Page<Announcement> page = new Page<>(1, 6);
        // 按时间降序排序
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_itme");
        // 如果查询出错，则返回空值
        try {
            announcementMapper.selectPage(page, queryWrapper);
        } catch (Exception e) {
            return null;
        }

        // 返回查询出来的结果
        return page.getRecords();
    }


    /**
     * 更多最新公告
     *
     * @param current 当前页码
     * @param size    每页显示数量
     * @return
     */
    @ResponseBody
    @GetMapping("/moreNewAnnouncement")
    public Page<Announcement> moreNewAnnouncement(@RequestBody Integer current, Integer size) {
        // 如果当前页码为空或者为0，默认设置第一页
        if (current == null || current <= 0) current = 1;
        // 如果每页显示数量为空或者为0，默认每页显示8个
        if (size == null || size <= 0) size = 8;

        Page<Announcement> page = new Page<>(current, size);
        // 按时间降序排序
        QueryWrapper<Announcement> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_itme");
        // 如果查询出错，则返回空值
        try {
            announcementMapper.selectPage(page, queryWrapper);
        } catch (Exception e) {
            return null;
        }

        // 分页对象
        return page;
    }

}
